package com.example.demoddd.application.services;

import com.example.demoddd.domain.userauth.model.User;
import com.example.demoddd.domain.userauth.services.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

@RequiredArgsConstructor
@Validated
@Service
public class UserServiceApplicationService implements UserDetailsService {
    private final UserService userService;
    

    /**
     * @param username 形参 用户唯一性字段  入参和 登录时UsernamePasswordAuthenticationToken 保持 一致
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("User not found");
        }
        return user;
    }
}




